{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3e8cae47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 : 2792 , 1396.0\n",
      "1 : 576 , 343.50461411476135\n",
      "2 : 4 , 2.8284270763397217\n",
      "3 : 4 , 2.8284270763397217\n",
      "4 : 568 , 287.3137083053589\n",
      "5 : 76 , 39.65685415267944\n",
      "6 : 688 , 388.73506212234497\n",
      "7 : 270 , 160.2670258283615\n",
      "8 : 682 , 433.36961901187897\n",
      "9 : 1172 , 587.6568541526794\n",
      "10 : 66 , 39.21320307254791\n",
      "11 : 66 , 39.21320307254791\n",
      "12 : 66 , 39.21320307254791\n",
      "13 : 66 , 39.21320307254791\n",
      "14 : 66 , 39.21320307254791\n",
      "15 : 66 , 39.21320307254791\n",
      "16 : 66 , 39.21320307254791\n"
     ]
    }
   ],
   "source": [
    "#周长\n",
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "filePath = '../data/multObj.png'\n",
    "img = cv.imread(filePath)\n",
    "imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "\n",
    "thresh, maxValue = 127, 255\n",
    "threshType = cv.THRESH_BINARY\n",
    "ret, thresh = cv.threshold(imgGray, thresh, maxValue, threshType)\n",
    "\n",
    "retrievalMode = cv.RETR_TREE\n",
    "approxMethod = cv.CHAIN_APPROX_NONE\n",
    "contours, hierachy = cv.findContours(thresh, retrievalMode, approxMethod)\n",
    "\n",
    "# 40: little circle \n",
    "# 160: question\n",
    "SymbolPerimeter = 160\n",
    "offset = round(SymbolPerimeter*0.2)\n",
    "validIdx = []\n",
    "for idx, cnt in enumerate(contours):\n",
    "    perimeter = cv.arcLength(cnt,True)   #true表示闭合的轮廓\n",
    "    print(idx, \":\", cnt.size, \",\", perimeter)\n",
    "    \n",
    "    minP = SymbolPerimeter - offset\n",
    "    maxP = SymbolPerimeter + offset\n",
    "    if perimeter > minP and perimeter < maxP:\n",
    "        validIdx.append(idx)\n",
    "\n",
    "imgBackGround = np.zeros(img.shape, np.uint8)\n",
    "\n",
    "for idx in validIdx:\n",
    "    cv.drawContours(imgBackGround, contours, idx, (0,0,255), 1)\n",
    "\n",
    "cv.imshow(\"img\", img)\n",
    "cv.imshow(\"imgBG\", imgBackGround)\n",
    "cv.waitKey(0)\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "39d64209",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 : 3784 , 203848.0\n",
      "1 : 8 , 2.0\n",
      "2 : 8 , 2.0\n",
      "3 : 8 , 2.0\n",
      "4 : 8 , 2.0\n",
      "5 : 12 , 4.0\n",
      "6 : 8 , 2.0\n",
      "7 : 1556 , 11560.0\n",
      "8 : 1352 , 44765.0\n"
     ]
    }
   ],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "filePath = '../data/circles.png'\n",
    "img = cv.imread(filePath)\n",
    "imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "\n",
    "thresh, maxValue = 127, 255\n",
    "threshType = cv.THRESH_BINARY\n",
    "ret, thresh = cv.threshold(imgGray, thresh, maxValue, threshType)\n",
    "\n",
    "retrievalMode = cv.RETR_TREE\n",
    "approxMethod = cv.CHAIN_APPROX_NONE\n",
    "contours, hierachy = cv.findContours(thresh, retrievalMode, approxMethod)\n",
    "\n",
    "#area\n",
    "# 44700: circle \n",
    "# 11560: moon\n",
    "#perimeter\n",
    "# 910: circle\n",
    "# 790: moon\n",
    "SymbolArea = 11560\n",
    "offset = round(SymbolArea*0.2)\n",
    "validIdx = []\n",
    "for idx, cnt in enumerate(contours):\n",
    "    area = cv.contourArea(cnt)     #轮廓围住的面积\n",
    "#     area = cv.arcLength(cnt,True)\n",
    "    print(idx, \":\", cnt.size, \",\", area)\n",
    "    \n",
    "    minP = SymbolArea - offset\n",
    "    maxP = SymbolArea + offset\n",
    "    if area > minP and area < maxP:\n",
    "        validIdx.append(idx)\n",
    "\n",
    "imgBackGround = np.zeros(img.shape, np.uint8)\n",
    "\n",
    "for idx in validIdx:\n",
    "    cv.drawContours(imgBackGround, contours, idx, (0,0,255), 1)\n",
    "\n",
    "cv.imshow(\"img\", img)\n",
    "cv.imshow(\"imgBG\", imgBackGround)\n",
    "cv.waitKey(0)\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14d70ca1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
